var xinm = new Array();
	xinm[0] = "img/86001.jpg"
	
var phone = new Array();
	phone[0] = "崔楠 工号86001"

var nametxt = $('.slot');
var phonetxt = $('.name');
var pcount = xinm.length-1;//参加人数
var runing = true;
var trigger = true;
var inUser = (Math.floor(Math.random() * 10000)) % 5 + 1;
var num = 0;
var Lotterynumber = 5; //设置单次抽奖人数

var benlunNumber = 0;
var timer = 0;

$(function () {
	nametxt.css('background-image','url('+xinm[0]+')');
	phonetxt.html(phone[0]);

	var arr1 = [];
	var arr2 = [];
	var gameArr1 = [];
	var gameArr2 = [];
	$.get("lottery.php" , function(data){
		for(let i = 0 ; i < data.length ; i ++){
		// for(let i = 0 ; i < 22 ; i ++){
			arr1.push(data[i].img);
			arr2.push(data[i].name + '   工号：' + data[i].id);
		}
		xinm = arr1;
		phone = arr2;

		localStorage.setItem('xinm',JSON.stringify(xinm));
		localStorage.setItem('phone',JSON.stringify(phone));

		pcount = xinm.length-1;
	})
	$.get("game.php" , function(data){
		for(let i = 0 ; i < data.length ; i ++){
		// for(let i = 0 ; i < 22 ; i ++){
			gameArr1.push(data[i].img);
			gameArr2.push(data[i].name + '   工号：' + data[i].id);
		}

		gameArr1 = arr1;
		gameArr2 = arr2;
		localStorage.setItem('gameArr1',JSON.stringify(gameArr1));
		localStorage.setItem('gameArr2',JSON.stringify(gameArr2));

		pcount = gameArr1.length-1;
	})


});

// 选择抽奖项
// function choseLevel(){
	var btns = $("#choujiangView").children();
	for(let i = 0 ; i < btns.length ; i ++){
		btns.eq(i).bind('click' , function(){
			var url = btns.eq(i).css("background-image");
			if( url.indexOf("_act") == -1 ){
				url = url.replace(".png" , "_act.png");
			}else{
				url = url.replace("_act.png" , ".png");
				benlunNumber = 0;
			}

			btns.eq(0).css("background-image" , "url(img/btn4.png)");
			btns.eq(1).css("background-image" , "url(img/btn3.png)");
			btns.eq(2).css("background-image" , "url(img/btn2.png)");
			btns.eq(3).css("background-image" , "url(img/btn1.png)");
			btns.eq(4).css("background-image" , "url(img/game.png)");

			btns.eq(i).css("background-image" , url);
			if(i == 0 && url.indexOf("_act") != -1){
				benlunNumber = 5;
				$("#titleName").text("中 奖 名 单");
			}else if(i == 1 && url.indexOf("_act") != -1){
				benlunNumber = 3;
				$("#titleName").text("中 奖 名 单");
			}else if(i == 2 && url.indexOf("_act") != -1){
				benlunNumber = 2;
				$("#titleName").text("中 奖 名 单");
			}else if(i == 3 && url.indexOf("_act") != -1){
				benlunNumber = 1;
				$("#titleName").text("中 奖 名 单");
			}else if(i == 4 && url.indexOf("_act") != -1){
				benlunNumber = 3;
				$("#titleName").text("参 与 人 员");
			}

			$(".luck-user-list").html("");
			timer = 0;
		})

	}
// }


// 开始停止
function start() {
	// choseLevel()

	if(benlunNumber == 0){
		alert('请先选择奖项');
		return;
	}
	inUser = (Math.floor(Math.random() * 10000)) % benlunNumber + 1;
	Lotterynumber = benlunNumber;

	//
	var indexKey;
	for(let i = 0 ; i < btns.length ; i ++){
		var url = btns.eq(i).css("background-image");
		if( url.indexOf("_act") != -1 ){
			indexKey = i;
			break;
		}
	}
	if(indexKey == 4){
		xinm = JSON.parse(localStorage.getItem('gameArr1'));
		phone = JSON.parse(localStorage.getItem('gameArr2'));
	}else{
		xinm = JSON.parse(localStorage.getItem('xinm'));
		phone = JSON.parse(localStorage.getItem('phone'));
	}


	var audio = document.getElementById("test");
	if(runing){
		if(timer >= benlunNumber){
			alert("抽奖人数已满");
			return;
		}
		// 滚动
		runing = false;
		$('#start').text('停止(' + ( benlunNumber - timer ) + ')');
		startNum()
		timer += 1;

		audio.play();
	}else{
		// 停止
		runing = true;
		$('#start').text('开始(' + ( benlunNumber - timer ) + ')');
		stop()
		audio.pause();

		//打印中奖者名单
		$('.luck-user-list').prepend("<li><div class='portrait' style='background-image:url("+xinm[num]+")'></div><div class='luckuserName'>"+phone[num]+"</div></li>");
		$('.modality-list ul').append("<li><div class='luck-img' style='background-image:url("+xinm[num]+")'></div><p>"+phone[num]+"</p></li>");
		//将已中奖者从数组中"删除",防止二次中奖
		xinm.splice($.inArray(xinm[num], xinm), 1);
		phone.splice($.inArray(phone[num], phone), 1);

		if(indexKey == 4){
			localStorage.setItem('gameArr1',JSON.stringify(xinm));
			localStorage.setItem('gameArr2',JSON.stringify(phone));
		}else{
			localStorage.setItem('xinm',JSON.stringify(xinm));
			localStorage.setItem('phone',JSON.stringify(phone));
		}
	}
	// if (runing) {
	// 	if ( pcount <= Lotterynumber ) {
	// 		alert("抽奖人数不足"+Lotterynumber+"人");
	// 	}else{
	// 		runing = false;
	// 		$('#start').text('停止011');
	// 		startNum()
	// 	}
	// } else {
	// 	// $('#start').text('自动抽取中('+ Lotterynumber+')');
	// 	zd();
	// }
	
}

// 开始抽奖
function startLuck() {
	// runing = false;
	$('#btntxt').removeClass('start').addClass('stop');
	startNum()
}

// 循环参加名单
function startNum() {
	num = Math.floor(Math.random() * pcount);
	nametxt.css('background-image','url('+xinm[num]+')');
	phonetxt.html(phone[num]);
	t = setTimeout(startNum, 80);
}

// 停止跳动
function stop() {
	pcount = xinm.length-1;
	clearInterval(t);
	t = 0;
}

// 打印中奖人
function zd() {
	if (trigger) {
		trigger = false;
		var i = 0;

		if ( pcount >= Lotterynumber ) {
			// stopTime = window.setInterval(function () {
				if (runing) {
					runing = false;
					$('#btntxt').removeClass('start').addClass('stop');
					$('#start').text('停止(' + Lotterynumber + ')');
					startNum();
				} else {
					runing = true;
					$('#btntxt').removeClass('stop').addClass('start');
					$('#start').text('开始22');
					stop();

					i++;
					Lotterynumber--;

					// $('#start').text('自动抽取中('+ Lotterynumber+')');

					if ( i == benlunNumber ) {
						console.log("抽奖结束");
						// window.clearInterval(stopTime);
						$('#start').text("开始123");
						Lotterynumber = benlunNumber;
						trigger = true;
					};

					//打印中奖者名单
					$('.luck-user-list').prepend("<li><div class='portrait' style='background-image:url("+xinm[num]+")'></div><div class='luckuserName'>"+phone[num]+"</div></li>");
					$('.modality-list ul').append("<li><div class='luck-img' style='background-image:url("+xinm[num]+")'></div><p>"+phone[num]+"</p></li>");
					//将已中奖者从数组中"删除",防止二次中奖
					xinm.splice($.inArray(xinm[num], xinm), 1);
					phone.splice($.inArray(phone[num], phone), 1);
				}
			// },1000);
		};
	}
}